﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using T.FAS.Action.Dev.Entity;
using T.FAS.Action.MetaData.Entity;

namespace T.FAS.Action.Dev.Dao
{
    public class DataResultConvert
    {
        public static List<ActionMetaDataView> ConvertToActionMetaDataView(DataSet ds)
        {
            if (ds == null || ds.Tables == null || ds.Tables.Count <= 0)
                return null;
            DataTable dt = ds.Tables[0];
            if (dt == null || dt.Rows==null || dt.Rows.Count<=0)
                return null;
            var list = new List<ActionMetaDataView>();
            foreach (DataRow row in dt.Rows)
            {
                var data = new ActionMetaDataView()
                {
                    ID = Convert.ToString(row["ID"]),
                    Code = Convert.ToString(row["Code"]),
                    Name = Convert.ToString(row["Name"]),
                    ReturnValueType = (ReturnValueType)Convert.ToInt16(row["ReturnValueType"] == DBNull.Value?0:row["ReturnValueType"]),
                    ActionType = (ActionType)Convert.ToInt16(row["ActionType"]),
                    Description = Convert.ToString(row["Description"]),
                    IsClientAction = Convert.ToInt16(row["IsClientAction"]).Equals(1) ? true : false,
                    RelatedMetaDataID = Convert.ToString(row["RelatedMetaDataID"])
                };
                list.Add(data);
            }
            return list;
        }

        public static List<ActionParamsView> ConvertToActionParamsView(DataSet ds)
        {
            if (ds == null || ds.Tables == null || ds.Tables.Count <= 0)
                return null;
            DataTable dt = ds.Tables[0];
            if (dt == null || dt.Rows == null || dt.Rows.Count <= 0)
                return null;
            var list = new List<ActionParamsView>();
            foreach (DataRow row in dt.Rows)
            {
                var data = new ActionParamsView()
                {
                    ID = Convert.ToString(row["ID"]),
                    Code = Convert.ToString(row["Code"]),
                    Name = Convert.ToString(row["Name"]),
                    SortOrder = Convert.ToInt16(row["SortOrder"]),
                    ParamType = (ParamType)Convert.ToInt16(row["ParamType"]),
                    ParamSourceType = (ParamSourceType)Convert.ToInt16(row["ParamSourceType"]),
                    SourceID = Convert.ToString(row["SourceID"]),
                    SourceName = Convert.ToString(row["SourceName"]),
                    ParamAssembly = Convert.ToString(row["ParamAssembly"]),
                    ParamValueType =Convert.ToString(row["ParamValueType"]),
                    DefaultValue = Convert.ToString(row["DefaultValue"]),
                    Description = Convert.ToString(row["Description"]),
                };
                list.Add(data);
            }
            return list;
        }
    }
}
